home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Development Tools & Languages / Dylan Related / Mindy / Mindy 1.2 - portable sources / etc / mindyexec.c < prev   
Encoding:
C/C++ Source or Header  |  1995-03-15  |  2.2 KB  |  99 lines  |  [TEXT/ttxt]

  1. /*
  2. ** mindyexec - execute dylan sources as scripts
  3. **
  4. ** execute dylan sources as scripts by starting a pipeline
  5. ** in which mindycomp compiles to standard output and mindy
  6. ** loads from standard input.
  7. **
  8. ** mindyexec is intended to be run as
  9. **
  10. **    mindyexec source-file [args-for-mindy-main ...]
  11. **
  12. ** which is the form in which the #! protocol will start.
  13. **
  14. ** mindyexec should be a mindy program when mindy knows how to do all
  15. ** this grotty system stuff.
  16. */
  17.  
  18. #include "../compat/std-c.h"
  19. #include "../compat/std-os.h"
  20.  
  21. #define MINDYCOMP "mindycomp"
  22.  
  23. int main(argc, argv) int argc; char *argv[];
  24. {
  25.   int fd[2], pid;
  26.   char *source;
  27.   
  28.   pipe(fd);
  29.  
  30.   if (pipe(fd) != 0) {
  31.     fprintf(stderr, "mindyexec: pipe failed: %s\n", strerror(errno));
  32.     exit(1);
  33.   }
  34.   
  35.   pid = fork();
  36.  
  37.   if (pid < 0) {
  38.     fprintf(stderr, "mindyexec: fork failed: %s\n", strerror(errno));
  39.     exit(1);
  40.   }
  41.  
  42.   if (argc < 2)
  43.     source = "-";            /* compile from standard input */
  44.   else
  45.     source = argv[1];            /* compile from script */
  46.  
  47.   if (pid == 0) {
  48.     static char *nargv[] = {
  49.       MINDYCOMP,            /* compile */
  50.       "source-script",            /* <source name here> */
  51.       "-q",                /* no warnings */
  52.       "-o",                /* output to */
  53.       "-",                /* stdout */
  54.       NULL
  55.     };
  56.  
  57.     nargv[1] = source;
  58.  
  59.     close(1);
  60.     dup(fd[1]);
  61.     close(fd[0]);
  62.     close(fd[1]);
  63.  
  64.     execvp(MINDYCOMP, nargv);
  65.  
  66.     fprintf(stderr, "mindyexec: execvp(\"%s\", ...) failed: %s\n", MINDYCOMP,
  67.         strerror(errno));
  68.     exit(1);
  69.   }
  70.  
  71.   if (pid > 0) {
  72.     char **nargv = malloc((argc+4+1) * sizeof(char *));
  73.     int nargc, oargc;
  74.  
  75.     nargc = 0;
  76.     nargv[nargc++] = "mindy";        /* interpret */
  77. #if ! NO_ARGV_0
  78.     nargv[nargc++] = "-0";        /* command name is */
  79.     nargv[nargc++] = source;        /* source file */
  80. #endif
  81.     nargv[nargc++] = "-x";        /* last file to load is */
  82.     nargv[nargc++] = "-";        /* stdin */
  83.     for (oargc = 2; oargc < argc; )
  84.       nargv[nargc++] = argv[oargc++];    /* rest of args for main() */
  85.     nargv[nargc] = NULL;
  86.  
  87.     close(0);
  88.     dup(fd[0]);
  89.     close(fd[0]);
  90.     close(fd[1]);
  91.  
  92.     execvp("mindy", nargv);
  93.  
  94.     fprintf(stderr, "mindyexec: execvp(\"mindy\", ...) failed:%s\n", strerror(errno));
  95.     exit(1);
  96.   }
  97. }
  98.  
  99.